/*
 * @Author: 春天被折进纸里 3301231308@qq.com
 * @Date: 2023-12-14 09:16:25
 * @LastEditors: 春天被折进纸里 3301231308@qq.com
 * @LastEditTime: 2024-04-01 14:26:35
 * @FilePath: \vue3-ts-pinia\src\utils\http.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import axios from "axios";

// 创建  一个axios 实例
const instace = axios.create({
  // baseURL: "api",
  timeout: 5000,
  headers: {
    "Content-Type": "application/json",
    // TOKEN: localStorage.getItem("token") || "",
  },
});

// 请求 拦截器

instace.interceptors.request.use(
  (config) => {
    // console.log("请求 拦截器", config);
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截器
instace.interceptors.response.use(
  (response) => {
    // console.log("响应 拦截器", response);
    return response.data;
  },
  (error) => {
    return Promise.reject(error);
  }
);

//
const callApi = (method = "GET", url = "", data = {}) => {
  return instace({
    method,
    url,
    params: method === "GET" ? data : {},
    data: method === "POST" ? JSON.stringify(data) : {},
  });
};

// get 请求方式
const getApi = (url = "", data = {}) => callApi("GET", url, data);
// post 请求方式
const postApi = (url = "", data = {}) => callApi("POST", url, data);

export { getApi, postApi };
